/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package bean;

import ejb.Customer;
import ejb.CustomerFacadeLocal;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author longdm0708l
 */
public class CustomerBean {

    public void createCustomer(Customer customer)
    {
        lookupCustomerFacade().create(customer);
    }

    public void editCustomer(Customer customer)
    {
        lookupCustomerFacade().edit(customer);
    }

    public void removeCustomer(Customer customer)
    {
        lookupCustomerFacade().remove(customer);
    }

    private int CustomerId;

    public int getCustomerId() {
        return CustomerId;
    }

    public void setCustomerId(int CustomerId) {
        this.CustomerId = CustomerId;
    }

    public List<Customer> getAllCustomer()
    {
        return lookupCustomerFacade().findAll();
    }

    public Customer getCustomerById()
    {
        return lookupCustomerFacade().find(Integer.valueOf(getCustomerId()));
    }

    private CustomerFacadeLocal lookupCustomerFacade() {
        try {
            Context c = new InitialContext();
            return (CustomerFacadeLocal) c.lookup("java:comp/env/CustomerFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

}
